
class DynamicMaterialProperty {

    constructor(opt) {
        this.definitionChanged = new Cesium.Event()
        this.isConstant = false
        this.defaultColor = new Cesium.Color(0, 0, 0, 0);
        opt = opt || {};
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this.color = opt.color || this.defaultColor; //颜色
        this._duration = opt.duration || 2000; //时长
        this.url = opt.url; //材质图片
        this.name = opt.name || "ooo";
        this._time = undefined;

        let rx = opt.repeatX
        let ry = opt.repeatY

        let myType=this.name


        Cesium.Material._materialCache.addMaterial(
            myType,
            {
                fabric: {
                    type: myType,
                    uniforms: {
                        color: new Cesium.Color(1, 0, 0, 1.0),
                        image: '',
                        time: 0,
                        repeat: new Cesium.Cartesian2(rx, ry)
                    },
                    source: `
                        czm_material czm_getMaterial(czm_materialInput materialInput)\n\
                        {\n\
                            czm_material material = czm_getDefaultMaterial(materialInput);\n\
                            vec2 st = repeat * materialInput.st;\n\
                            vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\
                            if(color.a == 0.0)\n\
                            {\n\
                                material.alpha = colorImage.a;\n\
                                material.diffuse = colorImage.rgb; \n\
                            }\n\
                            else\n\
                            {\n\
                                material.alpha = colorImage.a * color.a;\n\
                                material.diffuse = max(color.rgb * material.alpha * 3.0, color.rgb); \n\
                            }\n\
                            return material;\n\
                        }`
                },
                translucent: function translucent() {
                    return true;
                }
            }
        )
    }
    getType(time) {
        return this.name
    }
    getValue(time, result) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, this.defaultColor, result.color);
        result.image = this.url;
        if (this._time === undefined) {
            this._time = new Date().getTime();
        }
        result.time = (new Date().getTime() - this._time) / this._duration;

        return result;
    }
    equals(other) {
        // 判断两个材质是否相等
        return (
            other instanceof DynamicMaterialProperty && this.name === other.name
        )
    }

}
export default DynamicMaterialProperty